package LeetCode_622;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/26 11:16
 * //设计循环队列
 */
class MyCircularQueue {
    private int[] array;
    private int size;
    private int headIndex;
    private int lastIndex;

    public MyCircularQueue(int k) {
        array = new int[k];
        size = 0;
        headIndex = lastIndex = 0;
    }

    public boolean enQueue(int value) {
        //入队之前判满
        if (isFull()){
            return false;
        }
        array[lastIndex]=value;
        lastIndex = (lastIndex+1)%array.length;
        size++;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()){
            return false;
        }
        headIndex = (headIndex+1)%array.length;
        size--;
        return true;
    }

    public int Front() {
        if (isEmpty()){
            return  -1;
        }
        return array[headIndex];
    }

    public int Rear() {
        if (isEmpty()){
            return -1;
        }
        return array[(lastIndex-1 +array.length)%array.length];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == array.length;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */